package pATT.profilingAlgorithm.PersonalSearcher;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;



public class Cluster implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Hashtable						instances;
	private String							cluster_id;
	private TermVector						centroid = new TermVector();

	public Cluster(String l){
		instances=new Hashtable();
		cluster_id=l;
	}

	public String toString(){
		return instances.toString();
	}

	public TermVector getCentroid(){
		return centroid;
	}

	public TermVector getCentroidNormalized(){
		TermVector tnew = new TermVector();

		for (Iterator i=centroid.termIterator(); i.hasNext(); ) {
			String s = i.next().toString();
			double norm_value = centroid.get(s) / (double)size();
    			tnew.put(s,norm_value);
	    	}
		return tnew;
	}

	public boolean isEmpty(){
		return instances.isEmpty();
	}

	public Instance getInstance(String k){
		return (Instance)instances.get(k);
	}

	public double getIntraSimilarity(){
		if (getCentroidNormalized().size()>0)
			return Math.pow(getCentroidNormalized().getLength(),2);
		else
			return 1;
	}

	public void removeInstance(Instance i){
		instances.remove(i.getID());
		centroid.removeAll(i.getVector());
	}
	public String getID(){
		return cluster_id;
	}

	public int size(){
		return instances.size();
	}

	public Enumeration getEnumeration(){
		return instances.keys();
	}

	@SuppressWarnings("unchecked")
	public synchronized void addInstance(Instance i){
		instances.put(i.getID(),i);
		centroid.putAll(i.getVector());
	}

	//===================================================================================
	//   Para Hawk
	//===================================================================================

	public String getLabel(){
		return cluster_id;
	}

	public Enumeration keys(){
		return instances.keys();
	}
	
	public double getRelevance(){
		double sum = 0;
		for (Enumeration enum1 =instances.keys(); enum1.hasMoreElements() ;){
			String k = enum1.nextElement().toString();
			Instance cluster_instance = (Instance)instances.get(k);
			sum = sum + cluster_instance.getRelevance();
		}
		return sum;
	}
}
